package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;

import test.Company;

@ManagedBean(name="companyService", scope=ManagedBeanScope.SESSION)
public class CompanyService {
    
    private List<Company> companys = new ArrayList<Company>(Arrays.asList(new Company[]{
            new Company("1","Kingdee","金蝶ERP","深圳南山科技园","http://www.kingdee.com","400-836-836",""),
            new Company("2","Apusic", "Apusic Server" ,"深圳南山科技园","http://www.apusic.com","4008-830-830",""),
            new Company("3","Alcatel","数据网终","Paris, FRA","http://www.alcatel.com","330-140-7610",""),
            new Company("4","Tencent","QQ","南山腾讯大厦","http://www.qq.cn","86-75-83765566",""),
            new Company("5","SUN","Java系列产品","美国","http://www.sun.com","800-810-1818",""),
            new Company("6","NetEase","大话西游","天河软件园", "http://www.163.com","020-83568090",""),
            new Company("7","Sina","新浪资讯","北京市北四环西路", "http://www.sina.com.cn","8610-82628888",""),
            new Company("8","Sohu","搜狐门户","广州市珠江新城华夏路","http://www.sohu.com","8620-85115666",""),
            new Company("9","Dell","计算机","Round Rock, USA","http://www.dell.com","512-338-4400",""),
            new Company("10","Alibaba","电子商务","中国杭州市滨江区","http://www.alibaba.com","86-51-85022088",""),
            new Company("11","Huawei","电信解决方案","深圳市龙岗区坂田华为基地","http://www.huawei.com","0755-28780808",""),
            new Company("12","Google","搜索","北京清华科技园","http://www.google.com","86-10-62503000","")
    }));

    public List<Company> findAll() {
        return companys;
    }
    
    public Company findById(String id) {
        for (Company company : companys) {
            if (company.getId().equals(id)) {
                return company;
            }
        }
        return null;
    }

    public List<Company> findByName(String name) {
        List<Company> result = new ArrayList<Company>();
        for (Company company : companys) {
            if (company.getName().contains(name)) {
                result.add(company);
            }
        }
        return result;
    }

    public List<Company> findByProduct(String product) {
        List<Company> result = new ArrayList<Company>();
        for (Company company : companys) {
            if (company.getLeadingProduct().contains(product)) {
                result.add(company);
            }
        }
        return result;
    }

    public List<Company> getPageData(int start, int limit) {
        List<Company> result = new ArrayList<Company>();
        for (; limit > 0 && start < getTotalCount(); ++start, --limit) {
            result.add(companys.get(start));
        }
        return result;
    }

    public int getTotalCount() {
        return companys.size();
    }

    public boolean add(Company company) {
        return companys.add(company);
    }

    public boolean remove(Company company) {
        return companys.remove(findById(company.getId()));
    }

    public boolean update(Company newCompany) {
        Company oldCompany = findById(newCompany.getId());
        if (oldCompany != null) {
            oldCompany.setName(newCompany.getName());
            oldCompany.setLeadingProduct(newCompany.getLeadingProduct());
            oldCompany.setAddress(newCompany.getAddress());
            oldCompany.setHomepage(newCompany.getHomepage());
            oldCompany.setPhone(newCompany.getPhone());
            oldCompany.setRemark(newCompany.getRemark());
            return true;
        }
        return false;
    }
} 
